package poounit;
import java.lang.reflect.*;
import java.lang.Class;
import tec.TecInvalidException;

public class TestRun {

	private Class<?> p;
	private static int nbTest;
	private static int nbTestValide ;
	private static int nbTestEchoue ;
	private static int nbException ;	

    public static int getNbTest(){
	return nbTest;}
    public static int getNbTestValide(){
	return nbTestValide;}
    public static int getNbTestEchoue(){
	return nbTestEchoue
	    ;}
    public static int getNbException(){
	return nbException;}
    
    
    
    
    public TestRun(Class c){
	p=c;
    }
    
    public void run (){
	Class temp=this.p;
	Class temp1=temp.getSuperclass();
	while(null!=temp1){
	    if(null!=temp1.getSuperclass()){
		temp=temp1;
		temp1=temp.getSuperclass();
	    }
	    else{
		break;
	    }
	}
	Method[] m;
	Class[] args=null;
	Constructor cons;
	m = temp.getDeclaredMethods();
	for(Method tmp: m){
	    if(tmp.getAnnotation(Test.class) != null){
		System.out.print(".");
		nbTest ++;
		try{
		    tmp.setAccessible(true);
		    cons=this.p.getDeclaredConstructor(args);
		    cons.setAccessible(true);
		    tmp.invoke(cons.newInstance());
		    nbTestValide ++;
		}
		catch(NoSuchMethodException e){
		    
		    nbException++;
		    e.printStackTrace();
		}
		catch(InstantiationException e){
		    
		    nbException++;
		    e.printStackTrace();
		}
		catch(IllegalAccessException e){
		    e.printStackTrace();
		    nbException++;
		}
		catch(InvocationTargetException e){
		    nbException++;
		    
		    e.printStackTrace();
		}
		catch(AssertionError e){
		    nbTestEchoue++;
		    e.printStackTrace();
		}
		catch(IllegalArgumentException e){
		    nbException++;
		    e.printStackTrace();
		}
		catch(IllegalStateException e){
		    nbException++;
		    e.printStackTrace();
		}
		
	    }
	}
    }
}
